﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using SharingCenter.Web.Classes;
using System.Web.Security;
using SharingCenter.Web.Classes.Accounts;

namespace SharingCenter.Web.Admin
{
    public class AdminPageBase : Page
    {
        public AdminPageBase()
        {
            this.PreInit += new EventHandler(AdminPageBase_PreInit);
            this.InitComplete += new EventHandler(AdminPageBase_InitComplete);
        }

        void AdminPageBase_PreInit(object sender, EventArgs e)
        {
            //AuthenticationRequired();
        }

        void AdminPageBase_InitComplete(object sender, EventArgs e)
        {
            
        }

        protected void SetTopMostMessage(string text, bool pageUpdated)
        {
            PageBase.TopMostMessageText = text;
            PageBase.PageUpdated = pageUpdated;
        }


        protected void RedirectToDefaultPage()
        {
            Response.Redirect("~/");
        }

        protected void RefreshPage(bool addHashCode)
        {
            Response.Redirect(Request.Url.AbsolutePath + "?" + Utility.AddHashToQueryString(Request.QueryString));
        }

        protected void RefreshPage()
        {
            Response.Redirect(Request.Url.ToString());
        }

        public void AuthenticationRequired()
        {
            bool shallExitAdmin = false;
            if (!User.Identity.IsAuthenticated)
            {
                shallExitAdmin = true;
            }
            else
            {
                if (!RoleOperations.IsUserAdmin(User.Identity.Name))
                {
                    shallExitAdmin = true;
                }
            }

            if (shallExitAdmin)
            {
                FormsAuthentication.RedirectToLoginPage();
                Response.Flush();
                return;
            }
        }
    }
}
